package com.example.rightmost.controller.admin;

import com.example.rightmost.entity.Result;
import com.example.rightmost.entity.User;
import com.example.rightmost.service.UserService;
import com.example.rightmost.util.AliOssUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@RestController("adminUserController")
@RequestMapping("/admin")
public class UserController {

    @Autowired
    public UserService userService;

    //管理员登录
    @RequestMapping("/login")
    public Result login(@RequestBody User user){
        return userService.adminLogin(user);
    }

    //获取信息
    @RequestMapping("/getInfoById")
    public Result getInfoById(@RequestParam int id){
        return userService.getInfoById(id);
    }

    //修改信息
    @RequestMapping("/updateInfo")
    public Result updateInfo(@RequestBody User user){
        return userService.updateInfo(user);
    }

    //上传图片
    @RequestMapping("/uploadGoodImg")
    public String uploadGoodImg(MultipartFile file){
        if (null == file){
            return null;
        }
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath() + "static/upload/";
        System.out.println(path);
        path = path.substring(1);
        File dir = new File(path);
        if (!dir.exists()){ //如果文件目录不在
            dir.mkdir(); //创建文件目录
        }
        String imgName = UUID.randomUUID().toString() + file.getOriginalFilename();
        File imgf = new File(path + imgName);
        try {
            file.transferTo(imgf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return imgName;
    }

    @Autowired
    private AliOssUtil aliOssUtil;

    @RequestMapping("/upload")
    public String upload(MultipartFile file){
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;

            //文件的请求路径
            String filePath = aliOssUtil.upload(file.getBytes(), objectName);
            return filePath;
        } catch (IOException e) {
            System.out.println("文件上传失败");
        }

        return "文件上传失败";
    }

    //获取所有用户信息
    @RequestMapping("/getUserList")
    public PageInfo getUserList(@RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "5") int size,
                                @RequestBody User user
    ){
        PageHelper.startPage(page, size);
        List<User> list = userService.getUserList(user);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //删除用户
    @RequestMapping("/deleteUser")
    public Result deleteUser(@RequestParam int id){
        return userService.deleteUser(id);
    }

    //封号，解封
    @RequestMapping("/updateStatus")
    public Result updateStatus(@RequestParam int id){
        return userService.updateStatus(id);
    }

}

